<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<link rel="stylesheet" href="<%=request.getContextPath()%>/css/uniform.css" />
	</head>
	<body>
  <input type="button" class="btn btn-info" value="返回" id="return"></input>
  <div style="float:right;padding-right:20px;"><span class="label label-info">类别： ${relation_name }&nbsp;&nbsp;&nbsp;&nbsp;评估对象： ${assess_user_nm }</span></div>
  <br />
  <br />
		<s:hidden name="send_survey_user_assuser" id="id" ></s:hidden>
		<s:hidden name="relation" id="relation"></s:hidden>
		
		<!-- 问卷头部 -->
		<div class="widget-box">
			<!-- 问卷设计区 -->
			<div class="widget-content">				
				<div class="alert alert-block">
					<div class="error_ex">
						<h4 class="alert-heading">
							${survey.name}
						</h4>
					</div>
				</div>
				<div class="alert  alert-success alert-block">
					${survey.preface} 
				</div>
			<form action="/answer/user/submitAnswer.html" method="post" class="form-horizontal" id="answerForm">
				<div class="alert  alert-info alert-block" style="margin-top: 0px;">
					<strong>问卷回答区</strong>
					<div class="todo top-black">
						<ul id="survey_view">
							<s:iterator value="questionList" id="q" status="question">
								<li class="clearfix">
								<div class="txt top5" style="width:100%">
									<span style='font-weight:bold;font-size:15px;'>${question.index+1}、${q.name}</span>
									<s:if test="#q.required == 0"><font color="red">(必答题)</font></s:if>	
								</div>
									<br/>
									<div class="txt top5" style="width:100%">
										<s:iterator value="#q.optionList" id="o" status="option">
											<label style="padding-left: 20px;">
												<s:if test="#q.styleId == 1">
													<input type="radio" name="${q.id}"  style="margin:0px 5px" value="${o.id}" score="${o.score}" <s:if test="#o.id == #o.s_id">checked</s:if> required="${q.required}"/>
													${o.name}${o.content}
												</s:if>
												<s:if test="#q.styleId == 2">
													<input type="checkbox" name="${q.id}" style="margin:0px 5px" value="${o.id}" score="${o.score}" <s:if test="#o.id == #o.s_id">checked</s:if> required="${q.required}"/>
													${o.name}${o.content}
												</s:if>
											</label>
										</s:iterator>
											<s:if test="#q.styleId == 3">
												<s:textarea name="%{#q.id}" required="%{#q.required}"  cssStyle="width: 90%; height: 100px; margin: 10px 10px 10px;" value="%{#o.text}"></s:textarea>
											</s:if>
									</div>
								</li>
							</s:iterator>
						</ul>
					</div>
				</div>
				</form>
				<div class="alert  alert-success alert-block">
					${survey.afterword}
				</div>
			</div>
			 <div class="modal-footer">
           	<a href="#" class="btn" data-dismiss="modal" onclick="closeWindow();" >关闭</a>
           	<a href="#" class="btn" data-dismiss="modal" onclick="saveAnswer();return false;" >保存</a>
           	<a href="#" id="add-event-submit" class="btn btn-primary" onclick="submitAnswer();return false;" >提交</a>
           </div>
		</div>
	</body>
	
		<script type="text/javascript" src="<%=request.getContextPath()%>/js/comm/json2.js"></script>
		<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery/plugin/jquery.form.min.js"></script>
		<script type="text/javascript">
			jQuery(function(){
								
			  //返回事件
	    	jQuery("#return").bind("click",function(){
					var url = '<%=request.getContextPath()%>/answer/user/answerList.html';
					jQuery("#mainContent").fadeOut(600, function() {
				        $(this).load(url, function() {
				            $(this).fadeIn(600);
				        });;
			        });
			        
	    		//refMainContent('<%=request.getContextPath()%>/answer/user/answerList.html');
	    		return false;
	    	});
	    	
				jQuery(":radio").bind("click",function(){
					var id = jQuery("#id").val();
					var qId = jQuery(this).attr("name");
					var oId = jQuery(this).val();
					var relation = jQuery("#relation").val();
					var score = jQuery(this).attr("score");
					var checked = jQuery(this).attr("checked");
					var require = jQuery(this).attr("required");
					
					if ("checked" == checked){
						jQuery.post("<%=request.getContextPath()%>/answer/user/saveSingleQuestion.html",{
							id:id,
							qId:qId,
							oId:oId,
							relation:relation,
							score:score
						},function(data){
							if (!data){
								alert("系统错误！");
							}
						},"json");
					}
				});
				
				jQuery(":checkbox").bind("click",function(){
					
					var id = jQuery("#id").val();
					var qId = jQuery(this).attr("name");
					var oId = jQuery(this).val();
					var relation = jQuery("#relation").val();
					var score = jQuery(this).attr("score");
					var checked = jQuery(this).attr("checked");
					var url = "checked" == checked ? "<%=request.getContextPath()%>/answer/user/saveMultipleQuestion.html" : "<%=request.getContextPath()%>/answer/user/delMultipleAnswer.html";
					
					jQuery.post(url,{
						id:id,
						qId:qId,
						oId:oId,
						relation:relation,
						score:score
					},function(data){
						if (!data){
							alert("系统错误！");
						}
					},"json");
					
				});
				
				jQuery("textarea").bind("blur",function(){
					var qId = jQuery(this).attr("name");
					var id = jQuery("#id").val();
					var answer = jQuery(this).val();
					var relation = jQuery("#relation").val();
					
					jQuery.post("<%=request.getContextPath()%>/answer/user/saveTextQuestion.html?",{
						id:id,
						qId:qId,
						answer:answer,
						relation:relation
					},function(data){
						
						if (!data){
							alert("系统错误！");
						}
						
					},"json");
				});
				
			})
			
			function refMainContent(url){
				jQuery("#mainContent").load(url);
			}
			
			// 提交
			function submitAnswer(){

				var sname = "";
				var checked = "";
				var count = 0;
				jQuery("input[required='0']").each(function(){
					if(sname == "" || sname != jQuery(this).attr("name")){
							sname = jQuery(this).attr("name");
							checked = jQuery(":radio[name="+sname+"]:checked").val();
							if(checked == null || checked == ""){
								count +=1;
							}
					}
				});
				var valGet = "";
				jQuery("textarea[required='0']").each(function(){
							valGet = jQuery(this).val();
							if(valGet == null || valGet == ""){
								count +=1;
							}
				});
				if(count >0 ){
					alert("请回答所有必选题后再提交！");
				}else{
					var id = jQuery("#id").val();
					var relation = jQuery("#relation").val();
					
					jQuery.post("<%=request.getContextPath()%>/answer/user/submitAnswer.html?type=submit",{
						id:id,
						relation:relation
					},function(data){
						alert(data,function(){
							refMainContent('<%=request.getContextPath()%>/answer/user/answerList.html');
						});					
					},"json");
				}
			}

			// 保存
			function saveAnswer(){				
				
				var id = jQuery("#id").val();
				var relation = jQuery("#relation").val();
				
				jQuery.post("<%=request.getContextPath()%>/answer/user/submitAnswer.html?type=save",{
					id:id,
					relation:relation
				},function(data){
					alert(data,function(){
						//refMainContent('<%=request.getContextPath()%>/answer/user/answerList.html');
					});					
				},"json");
				
			}
			
		  // 关闭窗口
		 function closeWindow(){ 

				var _do = function(){
					 window.open('','_self','');
					 window.close(); 
				}
				confirm('您确定要关闭当前窗口吗？',_do);
		 }  
				   
		</script>
</html>
